#include <string.h>
#include <stdlib.h>
#include <getch.h>
#include "tools.h"

void clear_screen(void)
{
	system("clear");
}

void clear_stdin(void)
{
	stdin->_IO_read_ptr = stdin->_IO_read_end;	
}

char get_sex(void)
{
	clear_stdin();
	printf("请输入性别(男m/女w)：");
	for(;;)
	{
		char sex = getch();
		if('m'==sex || 'M'==sex || 'w'==sex || 'W'==sex)
		{
			printf("%s\n",show_sex(sex));
			return sex;
		}
	}
}

char* show_sex(char sex)
{
	switch(sex)
	{
	case 'm':
	case 'M': return "男";
	case 'w':
	case 'W': return "女";
	default:  return "X";
	}
}

char get_cmd(char start,char end)
{
	clear_stdin();
	printf("请输入指令：");
	for(;;)
	{
		char cmd = getch();
		if(start <= cmd && cmd <= end)
		{
			printf("%c\n",cmd);
			return cmd;
		}
	}
}

char* get_str(char* str,size_t size)
{
	clear_stdin();
	fgets(str,size,stdin);
	size_t len = strlen(str);

	if('\n' == str[len-1])
		str[len-1] = '\0';
	else
		clear_stdin();

	return str;
}

char* get_passwd(char* passwd,size_t size,bool flag)
{
	clear_stdin();
	size_t index = 0;
	while(index < size-1)
	{
		passwd[index] = getch();
		if(10 == passwd[index])
			break;
		if(127 == passwd[index])
		{
			if(index > 0)
			{
				index--;
				if(flag)
					printf("\b \b");
			}
			continue;
		}
		if(flag)
			printf("*");
		index++;
	}

	passwd[index] = '\0';
	printf("\n");
	return passwd;
}

void anykey_continue(void)
{
	clear_stdin();
	puts("按任意键继续...");
	getch();
}

bool yes_or_no(void)
{
	clear_stdin();
	printf("请选择(Y/N)：");
	for(;;)
	{
		char cmd = getch();
		if('y'==cmd || 'Y'==cmd)
			return true;
		if('n'==cmd || 'N'==cmd)
			return false;
	}
}

void show_msg(char* msg,float sec)
{
	show_color(msg,Blue);
	printf("\n");
	usleep(sec*1000000);
}

void show_err(char* err)
{
	show_color(err,Red);
	printf("\n");
}

void show_color(const char* msg,Color color)
{
	printf("\033[01;%dm%s\033[00m",color,msg);
	fflush(stdout);
}

void show_cursor(void)
{
	printf("\033[?25h");
	fflush(stdout);
}

void hide_cursor(void)
{
	printf("\033[?25l");
	fflush(stdout);
}

void set_cursor(int x,int y)
{
	printf("\033[%d;%dH",x,y);
	fflush(stdout);
}

void init_id(const char* path,uint64_t arr[],size_t size)
{
	if(access(path,F_OK))
		save_data(path,arr,sizeof(arr[0]),size);
}

uint64_t get_id(const char* path,size_t index)
{
	FILE* frwp = fopen(path,"r+");
	if(NULL == frwp)
	{
		show_err("末执行init_id函数，无法获取id！");
		return -1;
	}

	uint64_t id;
	fseek(frwp,index*sizeof(uint64_t),SEEK_SET);
	fread(&id,sizeof(uint64_t),1,frwp);
	id++;
	fseek(frwp,index*sizeof(uint64_t),SEEK_SET);
	fwrite(&id,sizeof(uint64_t),1,frwp);
	fclose(frwp);
	return id-1;
}

void save_data(const char* path,void* base,size_t size,size_t nmemb)
{
	FILE* fwp = fopen(path,"w");
	if(NULL == fwp)
	{
		show_err("文件的路径错误，无法创建文件！");
		return;
	}

	fwrite(base,size,nmemb,fwp);
	fclose(fwp);
}

size_t load_data(const char* path,void* base,size_t size,size_t nmemb)
{
	FILE* frp = fopen(path,"r");
	if(NULL == frp)
	{
		show_msg("暂无数据加载！",0.5);
		return 0;
	}

	size_t cnt = fread(base,size,nmemb,frp);
	fclose(frp);
	return cnt;
}

size_t file_size(const char* path)
{
	FILE* frp = fopen(path,"r");
	if(NULL == frp)
	{
		return -1;
	}

	fseek(frp,0,SEEK_END);
	size_t bytes = ftell(frp);
	fclose(frp);
	return bytes;
}

